package com.qinggeng.framework.config.mq.impl;

import com.alibaba.fastjson.JSON;
import com.qinggeng.common.enums.CommonEnum;
import com.qinggeng.common.enums.OrdersDistStatusEnum;
import com.qinggeng.common.enums.UserTypeEnum;
import com.qinggeng.common.exception.CustomException;
import com.qinggeng.framework.config.mq.MQSendUtils;
import com.qinggeng.framework.config.mq.dto.MessageOrdersDTO;
import com.qinggeng.framework.config.mq.dto.MessageOrdersMessageDTO;
import com.qinggeng.framework.config.mq.enums.MQTypeEnum;
import com.qinggeng.framework.config.mq.handle.MessageHandle;
import com.qinggeng.project.system.domain.Orders;
import com.qinggeng.project.system.service.IOrdersCourierService;
import com.qinggeng.project.system.service.IOrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@Component
public class OrdersDistAutoTakeHandleImpl implements MessageHandle {

    @Autowired
    private IOrdersService ordersService;

    @Autowired
    private IOrdersCourierService courierService;

    @Autowired
    private MQSendUtils mqSendUtils;

    @Override
    public void handle(String msg) {

        log.info("订单自动接单处理{}"+msg);

        MessageOrdersDTO ordersDTO= JSON.parseObject(msg,MessageOrdersDTO.class);

        Orders orders=ordersService.selectOrdersByOrdersNo(ordersDTO.getOrdersNo());
        if(null==orders){
            throw new CustomException("订单不存在");
        }
      /*  //10=未接单
        if(!CommonEnum.STATUS_10.getValue().equals(orders.getDistShopStatus())
                &&!OrdersDistStatusEnum.待接单.getValue().equals(orders.getDistStatus())){
            return;
        }*/
        try{
            if(!ordersService.ordersDistAutoTake(ordersDTO.getOrdersNo())){
                throw new CustomException("自动接单失败");
            }


        }catch (Exception e){
            throw new CustomException("自动接单失败");
        }

        //将订单骑手信息保存到数据库中
        if(courierService.insertOrdersCourier(orders.getOrdersId())<1){
            throw new CustomException("订单骑手保存失败");
        }
        //发送骑手订单推送消息
        this.sendCourierOrdersMessage(orders.getOrdersNo());
        //检验接单是否功
        this.checkIsSuccess(orders.getOrdersNo());
    }

    /**
     * 检验接单是否成功
     * @param ordersNo
     * @return
     */
    private boolean checkIsSuccess(String ordersNo){
        Orders orders=ordersService.selectOrdersByOrdersNo(ordersNo);
        if(null==orders){
            throw new CustomException("订单不存在");
        }
        if(null==orders.getDistShopStatus()||orders.getDistShopStatus().equals(CommonEnum.STATUS_10.getValue())){
            throw new CustomException("店铺接单失败");
        }
        if(null==orders.getDistStatus()||orders.getDistStatus().equals(OrdersDistStatusEnum.待接单.getValue())){
            throw new CustomException("骑手接单失败");
        }
        return true;
    }

    /**
     * 发送骑手订单推送消息
     * @param ordersNo
     */
    private void sendCourierOrdersMessage(String ordersNo){
        //发送消息，保存
        MessageOrdersMessageDTO ordersMessageDTO=new MessageOrdersMessageDTO();
        ordersMessageDTO.setType(MQTypeEnum.ORDERS_MESSAGE_JPUSH.getType());
        ordersMessageDTO.setUserType(UserTypeEnum.COURIER.getCode());
        ordersMessageDTO.setOrdersNo(ordersNo);
        ordersMessageDTO.setMark("订单接单通知");
        ordersMessageDTO.setMessage("尊敬的骑手阁下,您有新的订单"+ordersNo+"待取货!");
        mqSendUtils.sendAsynMessage(ordersMessageDTO);
    }

}
